package Service;


import Dao.GoodsDao;
import Dto.Goods;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsService {
    private GoodsDao goodsDao = new GoodsDao();
    public Map<String,Object> selectGoodsByPage(Integer pageNum) throws SQLException {
        List<Goods> list = goodsDao.selectCategoryList(pageNum);
        Long totalCount = goodsDao.selectCategoryCount();
        Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("totalCount",totalCount);
        map.put("totalPageNum",totalPageNum);
        return map;
    }
    //商品添加
    public void addGoods(Goods goods) throws SQLException {
        goodsDao.addGoods(goods);
    }
    //商品预修改
    public Goods preUpdateGoods(Integer id) throws SQLException {
        Goods goods = goodsDao.preUpdateGoods(id);
        return goods;
    }
    //商品修改
    public void updateGoods(Goods goods) throws SQLException {
        goodsDao.updateGoods(goods);
    }
    //商品删除
    public void deleteGoods(Integer id) throws SQLException {
        goodsDao.deleteGoods(id);
    }
    //查询
    public Map<String,Object> selectAll(String name,String category,String status) throws SQLException {
        Map<String,Object> map = new HashMap<>();
        if(name!=null&&!name.equals("")){
            List<Goods> nameList = goodsDao.selectGoodsName(name);
            Long totalCount = Long.valueOf(nameList.size());
            Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
            map.put("list",nameList);
            map.put("totalCount",totalCount);
            map.put("totalPageNum",totalPageNum);
            return map;
        }
        if(category!=null&&!category.equals("")){
            List<Goods> categoryList = goodsDao.selectGoodsCategory(category);
            Long totalCount = Long.valueOf(categoryList.size());
            Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
            map.put("list",categoryList);
            map.put("totalCount",totalCount);
            map.put("totalPageNum",totalPageNum);
            return map;
        }
        if(status!=null&&!status.equals("")){
            List<Goods> stateList = goodsDao.selectGoodsState(status);
            Long totalCount = Long.valueOf(stateList.size());
            Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
            map.put("list",stateList);
            map.put("totalCount",totalCount);
            map.put("totalPageNum",totalPageNum);
            return map;
        }
        List<Goods> list = goodsDao.selectAll();
        Long totalCount = Long.valueOf(list.size());
        Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
        map.put("list",list);
        map.put("totalCount",totalCount);
        map.put("totalPageNum",totalPageNum);
        return map;
    }
}